<?php
/*
 * @author : Thái Thanh Phong
 * @phone : 0933833174
 */
session_start();
class Tester extends CI_Controller{
    //put your code here
    
    public function __construct(){
        parent::__construct();
        $this->load->helper("url");
        $this->load->model('Export_csv_m');
        $this->load->helper('form');
        $this->load->model('Login_m');
        $this->load->model('Export_csv_m');
        $this->load->model('Download_m');
        $this->load->model('Comment_m');
        $this->load->model('Test_model');
        $this->load->model('Mark');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library("my_layout"); // Sử dụng thư viện layout
        $this->my_layout->setLayout("layout/tester_l"); // load file layout chính (view/layout/frontend.php)
    
    }
    // Controller mặc định cho trang chủ
    public function index(){
        $data['title'] = "ホームページ";
        $data['data'] = "ホームページのデータ";
        $session_data_sm = $this->session->userdata('logged_in_te');
	$data['id_te'] = $session_data_sm['id_te'];
        if($session_data_sm['id_te']!="")
        {
            $this->my_layout->view("frontend/home",$data);
        }
        else
        {
            $this->load->view('login/login_v');
        }
    }
    public function about(){
        $data['title'] = "紹介";
        $session_data_sm = $this->session->userdata('logged_in_te');
	$data['id_te'] = $session_data_sm['id_te'];
        $this->my_layout->view("frontend/about",$data);
    }
    public function forum(){
        $data['title'] = "フォーラム";
        $session_data_sm = $this->session->userdata('logged_in_te');
	$data['id_te'] = $session_data_sm['id_te'];
        $this->my_layout->view("frontend/forum",$data);
    }
    
    public function test(){
        $data1['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
	$data1['id_te'] = $session_data_sm['id_te'];
        $test_sub_title = $session_data_sm['test_sub_title'];
        $data1['test_sub_title']=$session_data_sm['test_sub_title'];
        $data1['test_title']=$session_data_sm['test_title'];
        $date = gmdate("Y-m-d-H-i-s", time()+7*3600);
        $date_str=  explode("-", $date);
        $year=$date_str[0];
        $month=$date_str[1];
        $day=$date_str[2];
        $hour=$date_str[3];
        $minute=$date_str[4];
        $second=$date_str[5];
        $start_time = $session_data_sm['start_time'];
        $manager_name = $session_data_sm['manager_name'];
        $data1['start_time']=$session_data_sm['start_time'];
        $date_str1=  explode("-", $start_time);
        $year1=$date_str1[0];
        $month1=$date_str1[1];
        $day1=$date_str1[2];
        $hour1=$date_str1[3];
        $minute1=$date_str1[4];
        $second1=$date_str1[5];
        $end_test_time=$session_data_sm['end_test_time'];
        $sum_second=$hour*3600+$minute*60+$second;
        $sum_second_test=$hour1*3600+$minute1*60+$second1;
        if(strlen($end_test_time)>6)
        {
            $date_str2=  explode("-", $end_test_time);
            $year2=$date_str2[0];
            $month2=$date_str2[1];
            $day2=$date_str2[2];
            $hour2=$date_str2[3];
            $minute2=$date_str2[4];
            $second2=$date_str2[5];
            $sum_second_test_end=$hour2*3600+$minute2*60+$second2;
            
        }
        else
        {
            $date_str3=  explode("-", $end_test_time);
            $second3=$date_str3[0];
            $minute3=$date_str3[1];
            if($minute3=="s")
            {
            $sum_second_test_end=$sum_second_test+$second3;
            }
            else if($minute3=="m")
            {
            $sum_second_test_end=$sum_second_test+$second3*60;    
            }
        }
        $links=$test_sub_title.":".$manager_name.":".$start_time.":".$end_test_time;
        $sess_array_links = array('links' => $links);
        $this->session->set_userdata('links_te', $sess_array_links);
        //$link_content = base_url()."login_tester/readLink/".$links;
        if($year==$year1&&$month==$month1&&$day==$day1)
        {
            
            if(($sum_second>=$sum_second_test)&&($sum_second<=$sum_second_test_end))
            {
                if($test_sub_title=="PTUF01-N01")
                {
                $data1['manager_name']=$session_data_sm['manager_name'];
                $query4=$this->Test_model->find_max_ques_f1();
                $data1['max_ques']=$query4->num_rows();
                $query=$this->Test_model->type_table_ques(1);
                foreach($query->result() as $row)
                {
                    $q=$row->q;
                }
                $query1=$this->Test_model->get_content_ques($q,1);
                 if($q=="f1_q1")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q1_v_f1_1',$data1);
                }
                else if($q=="f1_q2")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q2_v_f1_1',$data1);
                }
                else if($q=="f1_q3")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q3_v_f1_1',$data1);
                }
                else if($q=="f1_q4")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q4_v_f1_1',$data1);
                }
                else if($q=="f1_q5")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q5_v_f1_1',$data1);
                }
                else if($q=="f1_q6")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q6_v_f1_1',$data1);
                }
                else if($q=="f1_q7")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q7_v_f1_1',$data1);
                }
                }
                else if($test_sub_title=="PTUF02-N01")
                {
                $data1['manager_name']=$session_data_sm['manager_name'];
                $query4=$this->Test_model->find_max_ques_f2();
                $data1['max_ques']=$query4->num_rows();
                $query=$this->Test_model->type_table_ques1(1);
                foreach($query->result() as $row)
                {
                    $q=$row->q;
                }
                $query1=$this->Test_model->get_content_ques($q,1);
                 if($q=="f2_q1")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q1_v_f2_1',$data1);
                }
                else if($q=="f2_q2")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q2_v_f2_1',$data1);
                }
                else if($q=="f2_q3")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q3_v_f2_1',$data1);
                }
                else if($q=="f2_q4")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q4_v_f2_1',$data1);
                }
                else if($q=="f2_q5")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q5_v_f2_1',$data1);
                }
                else if($q=="f2_q6")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q6_v_f2_1',$data1);
                }
                else if($q=="f2_q7")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q7_v_f2_1',$data1);
                }
                else if($q=="f2_q8")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q8_v_f2_1',$data1);
                }    
                }
                else if($test_sub_title=="PTFX01-N01")
                {
                    $data1['manager_name']=$session_data_sm['manager_name'];
                    $query1=$this->Export_csv_m->select_x1_q_content('x1_q',1);
                    $query=$this->Export_csv_m->select_x1_q('x1_q');
                    $data1['max_ques']=$query->num_rows();
                    $data1['query1']=$query1;
                    $this->my_layout->view('tester/simulation_x1_1',$data1);
                }
                else if($test_sub_title=="PTFX02-N01")
                {
                    $data1['manager_name']=$session_data_sm['manager_name'];
                    $query1=$this->Export_csv_m->select_x1_q_content('x2_q',1);
                    $query=$this->Export_csv_m->select_x1_q('x2_q');
                    $data1['max_ques']=$query->num_rows();
                    $data1['query1']=$query1;
                    $this->my_layout->view('tester/simulation_x2_1',$data1);
                }
                else if($test_sub_title=="PTFX03-N01")
                {
                    $data1['manager_name']=$session_data_sm['manager_name'];
                    $query1=$this->Export_csv_m->select_x1_q_content('x3_q',1);
                    $query=$this->Export_csv_m->select_x1_q('x3_q');
                    $data1['max_ques']=$query->num_rows();
                    $data1['query1']=$query1;
                    $this->my_layout->view('tester/simulation_x3_1',$data1);
                }
            }
            else {
                if($sum_second<$sum_second_test)
                {
                 $this->my_layout->view('tester/test_v_1',$data1);
                }
                else if($sum_second>$sum_second_test_end) {
                    $this->my_layout->view('tester/test_v_2',$data1);
                }
             }
        }
        else
        {
            if($year1<$year)
            {
                $this->my_layout->view('tester/test_v_2',$data1);
            }
            else
            {
               $this->my_layout->view('tester/test_v_1',$data1); 
            }
            if($month1<$month)
            {
                $this->my_layout->view('tester/test_v_2',$data1);
            }
            else
            {
               $this->my_layout->view('tester/test_v_1',$data1); 
            }
            if($day1<$day)
            {
                $this->my_layout->view('tester/test_v_2',$data1);
            }
            else
            {
               $this->my_layout->view('tester/test_v_1',$data1); 
            }
        }
        
        
    }
    public function sub_ques_x1($id_next)
    {
        $data['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data['manager_name']=$session_data_sm['manager_name'];
	$data['id_te'] = $session_data_sm['id_te'];
        $test_sub_title_csv=$session_data_sm['test_sub_title'].".csv";
        $test_time=$session_data_sm['start_time'];
        $query4=$this->Export_csv_m->select_x1_q('x1_q');
        $data['max_ques']=$query4->num_rows();
        $id_question=$id_next-1;
        $sub_ans1=$this->input->post('select');
        
        $query2=$this->Export_csv_m->check_submit_x1($id_question,$session_data_sm['id_te'],$test_sub_title_csv);
                    if($query2->num_rows()>=1)
                    {
                        foreach($query2->result() as $row_an)
                        {
                            
                            $id_qsub=$row_an->id_qsub;
                        }
                        $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans1,$test_sub_title_csv);
                    }
                    else
                    {
                        
                        $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans1,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                          
                    }
   
                $querys1=$this->Export_csv_m->select_x1_q_content('x1_q',$id_next);
                $data['query1']=  $querys1;
                $this->my_layout->view('tester/simulation_x1_1',$data);
    }
    public function back_ques_x1($id_back)
    {
        $data['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data['id_te'] = $session_data_sm['id_te'];
        $data['manager_name']=$session_data_sm['manager_name'];
        $query4=$this->Export_csv_m->select_x1_q('x1_q');
        $data['max_ques']=$query4->num_rows();
        $id_question=$this->input->post('txtID');
        $querys=$this->Export_csv_m->select_x1_q_content('x1_q',$id_back);                  
        $data['query1']=  $querys;
        $this->my_layout->view('tester/simulation_x1_1',$data);
    }
    public function submit_x1($id_question)
    {
        $data['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data['id_te'] = $session_data_sm['id_te'];
        $data['manager_name']=$session_data_sm['manager_name'];
        $test_sub_title_csv=$session_data_sm['test_sub_title'].".csv";
        $test_time=$session_data_sm['start_time'];
        $data['test_sub_title']=$test_sub_title_csv;
        $query4=$this->Export_csv_m->select_x1_q('x1_q');
        $data['max_ques']=$query4->num_rows();
        $sub_ans1=$this->input->post('select');
        $query2=$this->Export_csv_m->check_submit_x1($id_question,$session_data_sm['id_te'],$test_sub_title_csv);
                    if($query2->num_rows()>=1)
                    {
                        foreach($query2->result() as $row_an)
                        {
                            $id_qsub=$row_an->id_qsub;
                        }
                        $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans1,$test_sub_title_csv);
                    }
                    else
                    {
                        $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans1,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                    }
                    
                    $data['success'] = 1;
                    $query5=$this->Mark->get_qsubmit($session_data_sm['id_te'],$test_sub_title_csv);
                    $mark=0;
                    $mark_i['mark']=array();
                    foreach($query5->result() as $rows5)
                    {
                        $query6=$this->Mark->get_answerx1_byid($rows5->id_question);
                        foreach($query6->result() as $rows6)
                        {
                            if($rows5->sub_an==$rows6->an1_ks)
                            {
                                $mark=$mark+$rows6->sc1;
                                $mark_i['mark'][$rows5->id_question]=$rows6->sc1;
                            }
                            else if($rows5->sub_an==$rows6->an2_ks)
                            {
                                $mark=$mark+$rows6->sc2;
                                $mark_i['mark'][$rows5->id_question]=$rows6->sc2;
                            }
                            else if($rows5->sub_an==$rows6->an3_ks)
                            {
                                $mark=$mark+$rows6->sc3;
                                $mark_i['mark'][$rows5->id_question]=$rows6->sc3;
                            }
                            else if($rows5->sub_an==$rows6->an4_ks)
                            {
                                $mark=$mark+$rows6->sc4;
                                $mark_i['mark'][$rows5->id_question]=$rows6->sc4;
                            }
                            else
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0;
                            }
                            $data['mark_i']=$mark_i;
                            $this->Mark->update_mark($rows5->id_question,$session_data_sm['id_te'],$test_sub_title_csv,$mark_i['mark'][$rows5->id_question]);
                        }
                    }
                    
                    $data['sum_mark']=$mark;    
                    $this->Mark->update_sum_mark($session_data_sm['id_te'],$session_data_sm['test_sub_title'],$mark);
                    $this->my_layout->view('tester/mark_x1',$data);
    }
    public function sub_ques_x2($id_next)
    {
        $data['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data['manager_name']=$session_data_sm['manager_name'];
        $data['id_te'] = $session_data_sm['id_te'];
        $test_time=$session_data_sm['start_time'];
        $test_sub_title_csv=$session_data_sm['test_sub_title'].".csv";
        $query4=$this->Export_csv_m->select_x1_q('x2_q');
        $data['max_ques']=$query4->num_rows();
        $id_question=$id_next-1;
        $sub_ans1=$this->input->post('select');
        $sub_time=$this->input->post('time');
        $query2=$this->Export_csv_m->check_submit_x1($id_question,$session_data_sm['id_te'],$test_sub_title_csv);
                    if($query2->num_rows()>=1)
                    {
                        foreach($query2->result() as $row_an)
                        {
                            
                            $id_qsub=$row_an->id_qsub;
                        }
                        $this->Export_csv_m->update_submit_x($session_data_sm['id_te'],$id_qsub,$sub_ans1,$test_sub_title_csv,$sub_time);
                    }
                    else
                    {
                        
                        $this->Export_csv_m->insert_answer_ques_tester($session_data_sm['id_te'],$id_question,$sub_ans1,$test_sub_title_csv,$session_data_sm['manager_name'],$sub_time,$test_time);
                          
                    }
   
                $querys1=$this->Export_csv_m->select_x1_q_content('x2_q',$id_next);
                $data['query1']=  $querys1;
                $this->my_layout->view('tester/simulation_x2_1',$data);
    }
    public function back_ques_x2($id_back)
    {
        $data['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data['id_te'] = $session_data_sm['id_te'];
        $data['manager_name']=$session_data_sm['manager_name'];
        $query4=$this->Export_csv_m->select_x1_q('x2_q');
        $data['max_ques']=$query4->num_rows();
        $id_question=$this->input->post('txtID');
        $querys=$this->Export_csv_m->select_x1_q_content('x2_q',$id_back);                  
        $data['query1']=  $querys;
        $this->my_layout->view('tester/simulation_x2_1',$data);
    }
    public function submit_x2($id_question)
    {
        $data['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data['id_te'] = $session_data_sm['id_te'];
        $data['manager_name']=$session_data_sm['manager_name'];
        $test_time=$session_data_sm['start_time'];
        $test_sub_title_csv=$session_data_sm['test_sub_title'].".csv";
        $data['test_sub_title']=$test_sub_title_csv;
        $query4=$this->Export_csv_m->select_x1_q('x2_q');
        $data['max_ques']=$query4->num_rows();
        $sub_ans1=$this->input->post('select');
        $sub_time=$this->input->post('time');
        $query2=$this->Export_csv_m->check_submit_x1($id_question,$session_data_sm['id_te'],$test_sub_title_csv);
                    if($query2->num_rows()>=1)
                    {
                        foreach($query2->result() as $row_an)
                        {
                            $id_qsub=$row_an->id_qsub;
                        }
                        $this->Export_csv_m->update_submit_x($session_data_sm['id_te'],$id_qsub,$sub_ans1,$test_sub_title_csv,$sub_time);
                    }
                    else
                    {
                        $this->Export_csv_m->insert_answer_ques_tester($session_data_sm['id_te'],$id_question,$sub_ans1,$test_sub_title_csv,$session_data_sm['manager_name'],$sub_time,$test_time);
                    }
                    
                    $data['success'] = 1;
                    $query5=$this->Mark->get_qsubmit($session_data_sm['id_te'],$test_sub_title_csv);
                    $mark=0;
                    $mark_i['mark']=array();
                    foreach($query5->result() as $rows5)
                    {
                        $query6=$this->Mark->get_answerx2_byid($rows5->id_question);
                        foreach($query6->result() as $rows6)
                        {
                            $trap=$rows6->TRAP;
                            $trap_array=  explode(",", $trap);
                            $trap_unchange=$trap_array[0];
                            $trap_triangle=$trap_array[1];
                            $trap_sum=$trap_triangle+$trap_unchange;
                            $mark_q=$rows6->sc;
                            if($rows5->sub_an==$rows6->an_ks)
                            {
                                if(($trap_sum-$sub_time)<=$trap_unchange)
                                {
                                $mark=$mark+$mark_q;
                                $mark_i['mark'][$rows5->id_question]=$mark_q;
                                }
                                else
                                {
                                $mark=$mark+($sub_time/$trap_triangle)*$mark_q;
                                $mark_i['mark'][$rows5->id_question]=($sub_time/$trap_triangle)*$mark_q;
                                }
                            }
                            else
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0;
                            }
                            $data['mark_i']=$mark_i;
                            $this->Mark->update_mark($rows5->id_question,$session_data_sm['id_te'],$test_sub_title_csv,$mark_i['mark'][$rows5->id_question]);
                        }
                    }
                    
                    $data['sum_mark']=$mark;    
                    $this->Mark->update_sum_mark($session_data_sm['id_te'],$session_data_sm['test_sub_title'],$mark);
                    $this->my_layout->view('tester/mark_x2',$data);
    }
    public function sub_ques_x3($id_next)
    {
        $data['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data['manager_name']=$session_data_sm['manager_name'];
	$data['id_te'] = $session_data_sm['id_te'];
        $test_sub_title_csv=$session_data_sm['test_sub_title'].".csv";
        $test_time=$session_data_sm['start_time'];
        $data['test_sub_title']=$test_sub_title_csv;
        $query4=$this->Export_csv_m->select_x1_q('x3_q');
        $data['max_ques']=$query4->num_rows();
        $id_question=$id_next-1;
        $sub_ans1=$this->input->post('select');
        $sub_time=$this->input->post('time');
        $query2=$this->Export_csv_m->check_submit_x1($id_question,$session_data_sm['id_te'],$test_sub_title_csv);
                    if($query2->num_rows()>=1)
                    {
                        foreach($query2->result() as $row_an)
                        {
                            
                            $id_qsub=$row_an->id_qsub;
                        }
                        $this->Export_csv_m->update_submit_x($session_data_sm['id_te'],$id_qsub,$sub_ans1,$test_sub_title_csv,$sub_time);
                    }
                    else
                    {
                        
                        $this->Export_csv_m->insert_answer_ques_tester($session_data_sm['id_te'],$id_question,$sub_ans1,$test_sub_title_csv,$session_data_sm['manager_name'],$sub_time,$test_time);
                          
                    }
   
                $querys1=$this->Export_csv_m->select_x1_q_content('x3_q',$id_next);
                $data['query1']=  $querys1;
                $this->my_layout->view('tester/simulation_x3_1',$data);
    }
    public function back_ques_x3($id_back)
    {
        $data['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data['id_te'] = $session_data_sm['id_te'];
        $data['manager_name']=$session_data_sm['manager_name'];
        $query4=$this->Export_csv_m->select_x1_q('x3_q');
        $data['max_ques']=$query4->num_rows();
        $id_question=$this->input->post('txtID');
        $querys=$this->Export_csv_m->select_x1_q_content('x3_q',$id_back);                  
        $data['query1']=  $querys;
        $this->my_layout->view('tester/simulation_x3_1',$data);
    }
    public function submit_x3($id_question)
    {
        $data['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data['id_te'] = $session_data_sm['id_te'];
        $data['manager_name']=$session_data_sm['manager_name'];
        $test_sub_title_csv=$session_data_sm['test_sub_title'].".csv";
        $test_time=$session_data_sm['start_time'];
        $data['test_sub_title']=$test_sub_title_csv;
        $query4=$this->Export_csv_m->select_x1_q('x3_q');
        $data['max_ques']=$query4->num_rows();
        $sub_ans1=$this->input->post('select');
        $sub_time=$this->input->post('time');
        $query2=$this->Export_csv_m->check_submit_x1($id_question,$session_data_sm['id_te'],$test_sub_title_csv);
                    if($query2->num_rows()>=1)
                    {
                        foreach($query2->result() as $row_an)
                        {
                            $id_qsub=$row_an->id_qsub;
                        }
                        $this->Export_csv_m->update_submit_x($session_data_sm['id_te'],$id_qsub,$sub_ans1,$test_sub_title_csv,$sub_time);
                    }
                    else
                    {
                        $this->Export_csv_m->insert_answer_ques_tester($session_data_sm['id_te'],$id_question,$sub_ans1,$test_sub_title_csv,$session_data_sm['manager_name'],$sub_time,$test_time);
                    }
                    
                    $data['success'] = 1;
                    $query5=$this->Mark->get_qsubmit($session_data_sm['id_te'],$test_sub_title_csv);
                    $mark=0;
                    $mark_i['mark']=array();
                    foreach($query5->result() as $rows5)
                    {
                        $query6=$this->Mark->get_answerx3_byid($rows5->id_question);
                        foreach($query6->result() as $rows6)
                        {
                            $tm1=$rows6->TM;
                            $tm1_array=  explode(",", $tm1);
                            $tmm=$tm1_array[0];
                            $tms=$tm1_array[1];
                            if($tms=="m")
                            {
                                $tm=$tmm*60;
                            }
                            else if($tms=="s")
                                $tm=$tmm;
                            $mark_q=$rows6->sc;
                            if($rows5->sub_an==$rows6->an_ks)
                            {
                                
                                $mark=$mark+($sub_time/$tm)*$mark_q;
                                $mark_i['mark'][$rows5->id_question]=($sub_time/$tm)*$mark_q;
                            }
                            else
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0;
                            }
                            $data['mark_i']=$mark_i;
                            $this->Mark->update_mark($rows5->id_question,$session_data_sm['id_te'],$test_sub_title_csv,$mark_i['mark'][$rows5->id_question]);
                        }
                    }
                    
                    $data['sum_mark']=$mark;  
                    $this->Mark->update_sum_mark($session_data_sm['id_te'],$session_data_sm['test_sub_title'],$mark);
                    $this->my_layout->view('tester/mark_x3',$data);
    }
    public function sub_ques_f2($id_next)
    {
        $data1['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data1['id_te'] = $session_data_sm['id_te'];
        $data1['manager_name']=$session_data_sm['manager_name'];
        $test_sub_title_csv=$session_data_sm['test_sub_title'].".csv";
        $test_time=$session_data_sm['start_time'];
        $query4=$this->Test_model->find_max_ques_f2();
        $data1['max_ques']=$query4->num_rows();
        $id_question=$id_next-1;
        $sub_ans1=$this->input->post('select');
        $data1['sub_ans1']=$sub_ans1;
        $sub_ans_text1=$this->input->post('text1');
        $data1['sub_ans_text1']=$sub_ans_text1;
        $sub_ans_text2=$this->input->post('text2');
        $data1['sub_ans_text2']=$sub_ans_text1;
        $sub_ans_text3=$this->input->post('text3');
        $data1['sub_ans_text3']=$sub_ans_text1;
        $sub_ans_text14=$this->input->post('text14');
        $data1['sub_ans_text4']=$sub_ans_text1;
        $sub_ans_text15=$this->input->post('text15');
        $data1['sub_ans_text15']=$sub_ans_text1;
        $sub_ans_text16=$this->input->post('text16');
        $data1['sub_ans_text16']=$sub_ans_text1;
        $sub_ans3=$this->input->post('checkbox');
        $data1['sub_ans3']=$sub_ans3;
        $sub_ans1417=$sub_ans_text14.",".$sub_ans_text15.",".$sub_ans_text16;
        //echo $sub_ans5."・".$sub_ans6."・".$sub_ans7;
        $sub_ans34="";
        for($j=0;$j<count($sub_ans3);$j++)
        {
            $sub_ans34=$sub_ans34.$sub_ans3[$j].",";
        }
        $sub_ans33=  substr($sub_ans34, 0, -1);
        $query2=$this->Export_csv_m->check_submit_x1($id_question,$session_data_sm['id_te'],$test_sub_title_csv);
                    if($query2->num_rows()>=1)
                    {
                        foreach($query2->result() as $row_an)
                        {
                            
                            $id_qsub=$row_an->id_qsub;
                            if($sub_ans1!=NULL)
                            {
                                $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans1,$test_sub_title_csv);
                            }
                            else if($sub_ans_text1!=NULL)
                            {
                                $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans_text1,$test_sub_title_csv);
                            }
                            else if($sub_ans33!=NULL)
                            {
                                $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans33,$test_sub_title_csv);
                                
                            }
                            else if($sub_ans_text2!=NULL)
                            {
                                $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans_text2,$test_sub_title_csv);
                            }
                            else if($sub_ans_text3!=NULL)
                            {
                                $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans_text3,$test_sub_title_csv);
                            }
                            else if($sub_ans1417!=NULL)
                            {
                                $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans1417,$test_sub_title_csv);
                            }
                            $sub_ans34=NULL;
                        }
                    }
                    else
                    {
                        
                        if($sub_ans1!=NULL)
                            {
                            $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans1,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                                //$this->Export_csv_m->update_submit_x1($session_data_sm['id_mk'],$id_qsub,$sub_ans1,$session_data_csv_file['file_name']);
                            }
                            else if($sub_ans_text1!=NULL)
                            {
                                $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans_text1,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                                //$this->Export_csv_m->update_submit_x1($session_data_sm['id_mk'],$id_qsub,$sub_ans1,$session_data_csv_file['file_name']);
                            }
                            else if($sub_ans33!=NULL)
                            {
                                //$this->Export_csv_m->update_submit_x1($session_data_sm['id_mk'],$id_qsub,$sub_ans1,$session_data_csv_file['file_name']);
                                $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans33,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                            }
                            else if($sub_ans_text2!=NULL)
                            {
                                $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans_text2,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                                //$this->Export_csv_m->update_submit_x1($session_data_sm['id_mk'],$id_qsub,$sub_ans1,$session_data_csv_file['file_name']);
                            }
                            else if($sub_ans_text3!=NULL)
                            {
                                $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans_text3,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                                //$this->Export_csv_m->update_submit_x1($session_data_sm['id_mk'],$id_qsub,$sub_ans1,$session_data_csv_file['file_name']);
                            }
                            else if($sub_ans1417!=NULL)
                            {
                                $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans1417,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                                //$this->Export_csv_m->update_submit_x1($session_data_sm['id_mk'],$id_qsub,$sub_ans1,$session_data_csv_file['file_name']);
                            }
              
                    }
                $querys=$this->Test_model->type_table_ques1($id_next);
                foreach($querys->result() as $rows)
                {
                    $q=$rows->q;
                }
                $query1=$this->Test_model->get_content_ques($q,$id_next);
                
                 if($q=="f2_q1")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q1_v_f2_1',$data1);
                }
                else if($q=="f2_q2")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q2_v_f2_1',$data1);
                }
                else if($q=="f2_q3")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q3_v_f2_1',$data1);
                }
                else if($q=="f2_q4")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q4_v_f2_1',$data1);
                }
                else if($q=="f2_q5")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q5_v_f2_1',$data1);
                }
                else if($q=="f2_q6")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q6_v_f2_1',$data1);
                }
                else if($q=="f2_q7")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q7_v_f2_1',$data1);
                }
                else if($q=="f2_q8")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q8_v_f2_1',$data1);
                }
                
    }
    public function back_ques_f2($id_back)
    {
        $data1['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data1['id_te'] = $session_data_sm['id_te'];
        $data1['manager_name']=$session_data_sm['manager_name'];
        $query4=$this->Export_csv_m->select_x1_q('x1_q');
        $data1['max_ques']=$query4->num_rows();
        $id_question=$this->input->post('txtID');
        $querys=$this->Test_model->type_table_ques1($id_back);
                foreach($querys->result() as $rows)
                {
                    $q=$rows->q;
                }
                $query1=$this->Test_model->get_content_ques($q,$id_back);
                 if($q=="f2_q1")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q1_v_f2_1',$data1);
                }
                else if($q=="f2_q2")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q2_v_f2_1',$data1);
                }
                else if($q=="f2_q3")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q3_v_f2_1',$data1);
                }
                else if($q=="f2_q4")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q4_v_f2_1',$data1);
                }
                else if($q=="f2_q5")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q5_v_f2_1',$data1);
                }
                else if($q=="f2_q6")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q6_v_f2_1',$data1);
                }
                else if($q=="f2_q7")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q7_v_f2_1',$data1);
                }
                else if($q=="f2_q8")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q8_v_f2_1',$data1);
                }
    }
    public function submit_f2($id_question)
    {
        $data['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data['id_te'] = $session_data_sm['id_te'];
        $data['manager_name']=$session_data_sm['manager_name'];
        $test_sub_title_csv=$session_data_sm['test_sub_title'].".csv";
        $test_time=$session_data_sm['start_time'];
        $data['test_sub_title']=$test_sub_title_csv;
        $query4=$this->Export_csv_m->select_x1_q('x1_q');
        $data['max_ques']=$query4->num_rows();
        $sub_ans_text14=$this->input->post('text14');
        $sub_ans_text15=$this->input->post('text15');
        $sub_ans_text16=$this->input->post('text16');
        $sub_ans1417=$sub_ans_text14.",".$sub_ans_text15.",".$sub_ans_text16;
        $query2=$this->Export_csv_m->check_submit_x1($id_question,$session_data_sm['id_te'],$test_sub_title_csv);
                    if($query2->num_rows()>=1)
                    {
                        foreach($query2->result() as $row_an)
                        {
                            $id_qsub=$row_an->id_qsub;
                        }
                        $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans1417,$test_sub_title_csv);
                    }
                    else
                    {
                        $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans1417,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                    }
                    
                    $data['success'] = 1;
                    $query5=$this->Mark->get_qsubmit($session_data_sm['id_te'],$test_sub_title_csv);
                    $mark=0;
                    $mark_i['mark']=array();
                    foreach($query5->result() as $rows5)
                    {
                        $text='f2_q';
                        $query_tb=$this->db->query("select * from compare where id_question=$rows5->id_question and q LIKE '".$text."%'");
                        foreach($query_tb->result() as $row1)
                        {
                            $q=$row1->q; 
                        }
                        $this->db->select('*');
                        $this->db->from($q);
                        $this->db->where('id_question',$rows5->id_question);
                        $query_q=$this->db->get();
                        foreach ($query_q->result() as $row_q) {
                        if($q=="f2_q1")
                        {
                            if($rows5->sub_an==$row_q->an_ks)
                            {
                                $mark=$mark+$row_q->sc;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc;
                            }
                            else 
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0;
                            }
                        }
                        else if($q=="f2_q2")
                        {
                            if($rows5->sub_an==$row_q->an1_ksa)
                            {
                                $mark=$mark+$row_q->sc1;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc1;
                            }
                            else if($rows5->sub_an==$row_q->an2_ksa)
                            {
                                $mark=$mark+$row_q->sc2;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc2;
                            }
                            else if($rows5->sub_an==$row_q->an3_ksa)
                            {
                                $mark=$mark+$row_q->sc3;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc3;
                            }
                            else 
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0;
                            } 
                        }
                        else if($q=="f2_q3")
                        {
                            if($rows5->sub_an==$row_q->an_ks)
                            {
                                $mark=$mark+$row_q->sc;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc;
                            }
                        else 
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0;
                            }
                        }
                        else if($q=="f2_q4")
                        {
                            if($rows5->sub_an==$row_q->an_kwa)
                            {
                                $mark=$mark+$row_q->sc;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc;
                            }
              
                            else 
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0;
                            }
                        }
                        else if($q=="f2_q5")
                        {
                            if($rows5->sub_an==$row_q->an1_ksa)
                            {
                                $mark=$mark+$row_q->sc1;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc1;
                            }
                            else if($rows5->sub_an==$row_q->an2_ksa)
                            {
                                $mark=$mark+$row_q->sc2;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc2;
                            }
                            else if($rows5->sub_an==$row_q->an3_ksa)
                            {
                                $mark=$mark+$row_q->sc3;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc3;
                            }
                            else if($rows5->sub_an==$row_q->an4_ksa)
                            {
                                $mark=$mark+$row_q->sc4;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc4;
                            }
                            else if($rows5->sub_an==$row_q->an5_ksa)
                            {
                                $mark=$mark+$row_q->sc5;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc5;
                            }
                            else 
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0;
                            } 
                        }
                        else if($q=="f2_q6")
                        {
                            $mark_i['mark'][$rows5->id_question]="採点者要介"; 
                        }
                        else if($q=="f2_q7")
                        {
                            $sub_an_l=  explode(",", $row_q->an1_kwpo);
                            $sub_an1=$sub_an_l[0];
                            $sub_an2=$sub_an_l[1];
                            if($rows5->sub_an==$sub_an1||$rows5->sub_an==$sub_an2)
                            {
                                $mark=$mark+$row_q->sc1;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc1;
                            }
                            else if($rows5->sub_an==$row_q->an2_kwp||$rows5->sub_an==$row_q->an3_kwp||$rows5->sub_an==$row_q->an4_kwp
                            ||$rows5->sub_an==$row_q->an5_kwp||$rows5->sub_an==$row_q->an6_kwp||$rows5->sub_an==$row_q->an7_kwp
                            ||$rows5->sub_an==$row_q->an8_kwp||$rows5->sub_an==$row_q->an9_kwp||$rows5->sub_an==$row_q->an10_kwp)
                            {
                                $mark=$mark+$row_q->sc2;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc2;
                            }
         
                            else
                            {
        
                                $mark_i['mark'][$rows5->id_question]="採点者要介"; 
                            }
                        }
                        else if($q=="f2_q8")
                        {
                            $sub_an_l=  explode(",", $rows5->sub_an);
                            $sub_an1=$sub_an_l[0];
                            $sub_an2=$sub_an_l[1];
                            $sub_an3=$sub_an_l[2];
                            $sub_an12=$sub_an1.",".$sub_an2;
                            $sub_an13=$sub_an1.",".$sub_an3;
                            $sub_an23=$sub_an2.",".$sub_an3;
                            if($sub_an12==$row_q->an1_kwpo||$sub_an13==$row_q->an1_kwpo||$sub_an23==$row_q->an1_kwpo)
                            {
                                $mark=$mark+$row_q->sc1;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc1;
                            }
                            else if($sub_an12==$row_q->an2_kwpo||$sub_an13==$row_q->an2_kwpo||$sub_an23==$row_q->an2_kwpo)
                            {
                                $mark=$mark+$row_q->sc2;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc2;
                            }
                            else if($sub_an12==$row_q->an3_kwpo||$sub_an13==$row_q->an3_kwpo||$sub_an23==$row_q->an3_kwpo)
                            {
                                $mark=$mark+$row_q->sc3;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc3;
                            }
                            else
                            {
                                $mark_i['mark'][$rows5->id_question]="採点者要介";
                            }
                            
                        }
                        $data['mark_i']=$mark_i;
                        $this->Mark->update_mark($rows5->id_question,$session_data_sm['id_te'],$test_sub_title_csv,$mark_i['mark'][$rows5->id_question]);
                    }
                   }
                    $data['sum_mark']=$mark; 
                    $this->Mark->update_sum_mark($session_data_sm['id_te'],$session_data_sm['test_sub_title'],$mark);
                    $this->my_layout->view('tester/mark_f2',$data);
    }
    public function sub_ques_f1($id_next)
    {
        $data1['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data1['id_te'] = $session_data_sm['id_te'];
        $data1['manager_name']=$session_data_sm['manager_name'];
        $test_sub_title_csv=$session_data_sm['test_sub_title'].".csv";
        $test_time=$session_data_sm['start_time'];
        $query4=$this->Test_model->find_max_ques_f1();
        $data1['max_ques']=$query4->num_rows();
        echo $query4->num_rows();
        $id_question=$id_next-1;
        $sub_ans1=$this->input->post('select');
        $sub_ans_text1=$this->input->post('text1');
        $sub_ans_text2=$this->input->post('text2');
        $sub_ans_text3=$this->input->post('text3');
        $sub_ans_text4=$this->input->post('text4');
        $sub_ans_text5=$this->input->post('text5');
        $sub_ans_text6=$this->input->post('text6');
        $sub_ans_text7=$this->input->post('text7');
        $sub_ans_text8=$this->input->post('text8');
        $sub_ans_text9=$this->input->post('text9');
        $sub_ans_text10=$this->input->post('text10');
        $sub_ans_text11=$this->input->post('text11');
        $sub_ans_text12=$this->input->post('text12');
        $sub_ans_text13=$this->input->post('text13');
        $sub_ans_text14=$this->input->post('text14');
        $sub_ans_text15=$this->input->post('text15');
        $sub_ans_text16=$this->input->post('text16');
        $sub_ans_text17=$this->input->post('text17');
        $sub_ans_text18=$this->input->post('text18');
        $sub_ans_text19=$this->input->post('text19');
        $sub_ans_text20=$this->input->post('text20');
        $sub_ans_text21=$this->input->post('text21');
        $sub_ans3=$this->input->post('checkbox');
        $sub_ans211=$sub_ans_text2.",".$sub_ans_text3.",".$sub_ans_text4.",".$sub_ans_text5
                .",".$sub_ans_text6.",".$sub_ans_text7.",".$sub_ans_text8.",".$sub_ans_text9
                .",".$sub_ans_text10.",".$sub_ans_text11;
        $sub_ans1221=$sub_ans_text12.",".$sub_ans_text13.",".$sub_ans_text14.",".$sub_ans_text15
                .",".$sub_ans_text16.",".$sub_ans_text17.",".$sub_ans_text18.",".$sub_ans_text19
                .",".$sub_ans_text20.",".$sub_ans_text21;
        //echo $sub_ans5."・".$sub_ans6."・".$sub_ans7;
        $sub_ans34="";
        for($j=0;$j<count($sub_ans3);$j++)
        {
            $sub_ans34=$sub_ans34.$sub_ans3[$j].",";
        }
        $sub_ans33=  substr($sub_ans34, 0, -1);
        $query2=$this->Export_csv_m->check_submit_x1($id_question,$session_data_sm['id_te'],$test_sub_title_csv);
                    if($query2->num_rows()>=1)
                    {
                        foreach($query2->result() as $row_an)
                        {
                            
                            $id_qsub=$row_an->id_qsub;
                            if($sub_ans1!=NULL)
                            {
                                $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans1,$test_sub_title_csv);
                            }
                            else if($sub_ans_text1!=NULL)
                            {
                                $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans_text1,$test_sub_title_csv);
                            }
                            else if($sub_ans33!=NULL)
                            {
                                $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans33,$test_sub_title_csv);
                                
                            }
                            else if($sub_ans211!=NULL)
                            {
                                $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans211,$test_sub_title_csv);
                            }
                            else if($sub_ans1221!=NULL)
                            {
                                $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans1221,$test_sub_title_csv);
                            }
                            $sub_ans34=NULL;
                        }
                    }
                    else
                    {
                        
                        if($sub_ans1!=NULL)
                            {
                            $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans1,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                                //$this->Export_csv_m->update_submit_x1($session_data_sm['id_mk'],$id_qsub,$sub_ans1,$session_data_csv_file['file_name']);
                            }
                            else if($sub_ans_text1!=NULL)
                            {
                                $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans_text1,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                                //$this->Export_csv_m->update_submit_x1($session_data_sm['id_mk'],$id_qsub,$sub_ans1,$session_data_csv_file['file_name']);
                            }
                            else if($sub_ans33!=NULL)
                            {
                                //$this->Export_csv_m->update_submit_x1($session_data_sm['id_mk'],$id_qsub,$sub_ans1,$session_data_csv_file['file_name']);
                                $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans33,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                            }
                            else if($sub_ans211!=NULL)
                            {
                                $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans211,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                                //$this->Export_csv_m->update_submit_x1($session_data_sm['id_mk'],$id_qsub,$sub_ans1,$session_data_csv_file['file_name']);
                            }
                            else if($sub_ans1221!=NULL)
                            {
                                $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans1221,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                                //$this->Export_csv_m->update_submit_x1($session_data_sm['id_mk'],$id_qsub,$sub_ans1,$session_data_csv_file['file_name']);
                            }
              
                    }
                $querys=$this->Test_model->type_table_ques($id_next);
                foreach($querys->result() as $rows)
                {
                    $q=$rows->q;
                }
                $query1=$this->Test_model->get_content_ques($q,$id_next);
                
                 if($q=="f1_q1")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q1_v_f1_1',$data1);
                }
                else if($q=="f1_q2")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q2_v_f1_1',$data1);
                }
                else if($q=="f1_q3")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q3_v_f1_1',$data1);
                }
                else if($q=="f1_q4")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q4_v_f1_1',$data1);
                }
                else if($q=="f1_q5")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q5_v_f1_1',$data1);
                }
                else if($q=="f1_q6")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q6_v_f1_1',$data1);
                }
                else if($q=="f1_q7")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q7_v_f1_1',$data1);
                }
                
    }
    public function back_ques_f1($id_back)
    {
        $data1['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data1['id_te'] = $session_data_sm['id_te'];
        $data1['manager_name']=$session_data_sm['manager_name'];
        $query4=$this->Export_csv_m->select_x1_q('x1_q');
        $data1['max_ques']=$query4->num_rows();
        $id_question=$this->input->post('txtID');
        $querys=$this->Test_model->type_table_ques($id_back);
                foreach($querys->result() as $rows)
                {
                    $q=$rows->q;
                }
                $query1=$this->Test_model->get_content_ques($q,$id_back);
                 if($q=="f1_q1")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q1_v_f1_1',$data1);
                }
                else if($q=="f1_q2")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q2_v_f1_1',$data1);
                }
                else if($q=="f1_q3")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q3_v_f1_1',$data1);
                }
                else if($q=="f1_q4")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q4_v_f1_1',$data1);
                }
                else if($q=="f1_q5")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q5_v_f1_1',$data1);
                }
                else if($q=="f1_q6")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q6_v_f1_1',$data1);
                }
                else if($q=="f1_q7")
                {
                    $data1['query_ques2']=  $query1->result();
                    $this->my_layout->view('tester/q7_v_f1_1',$data1);
                }
    }
    public function submit_f1($id_question)
    {
        $data['title'] = "テスト";
        $session_data_sm = $this->session->userdata('logged_in_te');
        $data['id_te'] = $session_data_sm['id_te'];
        $data['manager_name']=$session_data_sm['manager_name'];
        $test_sub_title_csv=$session_data_sm['test_sub_title'].".csv";
        $test_time=$session_data_sm['start_time'];
        $data['test_sub_title']=$test_sub_title_csv;
        $query4=$this->Export_csv_m->select_x1_q('x1_q');
        $data['max_ques']=$query4->num_rows();
        $sub_ans_text12=$this->input->post('text12');
        $sub_ans_text13=$this->input->post('text13');
        $sub_ans_text14=$this->input->post('text14');
        $sub_ans_text15=$this->input->post('text15');
        $sub_ans_text16=$this->input->post('text16');
        $sub_ans_text17=$this->input->post('text17');
        $sub_ans_text18=$this->input->post('text18');
        $sub_ans_text19=$this->input->post('text19');
        $sub_ans_text20=$this->input->post('text20');
        $sub_ans_text21=$this->input->post('text21');
        $sub_ans1221=$sub_ans_text12.",".$sub_ans_text13.",".$sub_ans_text14.",".$sub_ans_text15
                .",".$sub_ans_text16.",".$sub_ans_text17.",".$sub_ans_text18.",".$sub_ans_text19
                .",".$sub_ans_text20.",".$sub_ans_text21;
        $query2=$this->Export_csv_m->check_submit_x1($id_question,$session_data_sm['id_te'],$test_sub_title_csv);
                    if($query2->num_rows()>=1)
                    {
                        foreach($query2->result() as $row_an)
                        {
                            $id_qsub=$row_an->id_qsub;
                        }
                        $this->Export_csv_m->update_submit_x1($session_data_sm['id_te'],$id_qsub,$sub_ans1221,$test_sub_title_csv);
                    }
                    else
                    {
                        $this->Export_csv_m->insert_answer_ques1_x1_tester($session_data_sm['id_te'],$id_question,$sub_ans1221,$test_sub_title_csv,$session_data_sm['manager_name'],$test_time);
                    }
                    
                    $data['success'] = 1;
                    $query5=$this->Mark->get_qsubmit($session_data_sm['id_te'],$test_sub_title_csv);
                    $mark=0;
                    $mark_i['mark']=array();
                    foreach($query5->result() as $rows5)
                    {
                        $text='f1_q';
                        $query_tb=$this->db->query("select * from compare where id_question=$rows5->id_question and q LIKE '".$text."%'");
                        foreach($query_tb->result() as $row1)
                        {
                            $q=$row1->q; 
                        }
                        $this->db->select('*');
                        $this->db->from($q);
                        $this->db->where('id_question',$rows5->id_question);
                        $query_q=$this->db->get();
                        foreach ($query_q->result() as $row_q) {
                        if($q=="f1_q1")
                        {
                            if($rows5->sub_an==$row_q->an_ks)
                            {
                                $mark=$mark+$row_q->sc;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc;
                            }
                            else 
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0;
                            }
                            
                        }
                        else if($q=="f1_q2")
                        {
                            $sub_an_l=  explode(",", $row_q->an_kso);
                            $sub_an1=$sub_an_l[0];
                            $sub_an2=$sub_an_l[1];
                            if($rows5->sub_an==$row_q->an_kso||$rows5->sub_an==$sub_an1||$rows5->sub_an==$sub_an2)
                            {
                                $mark=$mark+$row_q->sc;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc;
                            }
                            else
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0; 
                            }
                        }
                        else if($q=="f1_q3")
                        {
                            if($rows5->sub_an==$row_q->an_ksa)
                            {
                                $mark=$mark+$row_q->sc;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc;
                            }
                            else
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0; 
                            }
                        }
                        else if($q=="f1_q4")
                        {
                            if($rows5->sub_an==$row_q->an1_kw)
                            {
                                $mark=$mark+$row_q->sc_an1;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc_an1;
                            }
                            else if($rows5->sub_an==$row_q->an2_kw)
                            {
                                $mark=$mark+$row_q->sc_an2;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc_an2;
                            }
                            else 
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0;
                            } 
                        }
                        
                        else if($q=="f1_q5")
                        {
                            $sub_an_l=  explode(",", $row_q->an4_kso);
                            $sub_an1=$sub_an_l[0];
                            $sub_an2=$sub_an_l[1];
                            $sub_an3=$sub_an_l[2];
                            $sub_an4=$sub_an_l[3];
                            $sub_an5=$sub_an_l[4];
                            if($rows5->sub_an==$row_q->an1_ks)
                            {
                                $mark=$mark+$row_q->sc1;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc1;
                            }
                            else if($rows5->sub_an==$row_q->an2_ks)
                            {
                                $mark=$mark+$row_q->sc2;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc2;
                            }
                            if($rows5->sub_an==$row_q->an3_ksa)
                            {
                                $mark=$mark+$row_q->sc3;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc3;
                            }
                            if(strlen(strstr($row_q->an4_kso,$rows5->sub_an)) > 0)
                            {
                                $mark=$mark+$row_q->sc4;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc4;
                            }
                            else
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0; 
                            }
                        }
                        else if($q=="f1_q6")
                        {
                            $sub_an_lo=  explode(",", $row_q->an1_kwpo);
                            $sub_an1o=$sub_an_lo[0];
                            $sub_an2o=$sub_an_lo[1];
                            $sub_an3o=$sub_an_lo[2];
                            $sub_an_lt=  explode(",", $row_q->an3_kwpo);
                            $sub_an1t=$sub_an_lt[0];
                            $sub_an2t=$sub_an_lt[1];
                            $sub_an3t=$sub_an_lt[2];
                            $sub_an_lf=  explode(",", $row_q->an4_kwpo);
                            $sub_an1f=$sub_an_lf[0];
                            $sub_an2f=$sub_an_lf[1];
                            
                            if($rows5->sub_an==$sub_an1o||$rows5->sub_an==$sub_an2o||$rows5->sub_an==$sub_an3o)
                            {
                                $mark=$mark+$row_q->sc1;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc1;
                            }
                            else if($rows5->sub_an==$sub_an1f||$rows5->sub_an==$sub_an2f)
                            {
                                $mark=$mark+$row_q->sc4;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc4;
                            }
                            else if($rows5->sub_an==$sub_an1t||$rows5->sub_an==$sub_an2t||$rows5->sub_an==$sub_an3t)
                            {
                                $mark=$mark+$row_q->sc3;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc3;
                            }
                            else if($rows5->sub_an==$row_q->an2_kwp
                            ||$rows5->sub_an==$row_q->an5_kwp||$rows5->sub_an==$row_q->an6_kwp||$rows5->sub_an==$row_q->an7_kwp
                            ||$rows5->sub_an==$row_q->an8_kwp||$rows5->sub_an==$row_q->an9_kwp||$rows5->sub_an==$row_q->an10_kwp)
                            {
                                $mark=$mark+$row_q->sc2;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc2;
                            }
         
                            else
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0; 
                            } 
                        }
                        else if($q=="f1_q7")
                        {
                            if($rows5->sub_an==$row_q->an1_kwp||$rows5->sub_an==$row_q->an2_kwp||$rows5->sub_an==$row_q->an3_kwp||$rows5->sub_an==$row_q->an4_kwp
                            ||$rows5->sub_an==$row_q->an5_kwp||$rows5->sub_an==$row_q->an6_kwp||$rows5->sub_an==$row_q->an7_kwp
                            ||$rows5->sub_an==$row_q->an8_kwp||$rows5->sub_an==$row_q->an9_kwp||$rows5->sub_an==$row_q->an10_kwp)
                            {
                                $mark=$mark+$row_q->sc1;
                                $mark_i['mark'][$rows5->id_question]=$row_q->sc1;
                            }
         
                            else
                            {
                                $mark=$mark+0;
                                $mark_i['mark'][$rows5->id_question]=0; 
                            } 
                        }
                       $data['mark_i']=$mark_i;
                       $this->Mark->update_mark($rows5->id_question,$session_data_sm['id_te'],$test_sub_title_csv,$mark_i['mark'][$rows5->id_question]);
                      }  
                   }
                    $data['sum_mark']=$mark;
                    $this->Mark->update_sum_mark($session_data_sm['id_te'],$session_data_sm['test_sub_title'],$mark);
                    $this->my_layout->view('tester/mark_f1',$data);
    }
    function download()
    {
        $data['title']="ダウンロード";
        $session_data_sm = $this->session->userdata('logged_in_te');
	$data['id_te'] = $session_data_sm['id_te'];
        $data['user'] = "examiner";
        $this->load->helper(array('form'));
        $this->my_layout->view('download/download_v',$data); 
    }
    function verifydownload()
    {
        $data['title']="ダウンロード";
        $session_data_sm = $this->session->userdata('logged_in_te');
	$data['id_te'] = $session_data_sm['id_te'];
        $filedown=$this->input->post('filedown');
        $name=$this->Download_m->getCSVfromDB($filedown);
        $data['name']=$name;
        foreach ($name as $rows) {
        ////code hieu
            $session_array_download=array('name'=>$rows->name,'link'=>$rows->link);
            $this->session->set_userdata('download_file',$session_array_download);       
        }
        $this->my_layout->view('download/downfile_v',$data);
    }
    public function downfile($id)
    {
        //$session_download = $this->session->userdata('download_file');
        //$test=$session_download['name'];
        //$test=$_GET['STT'];
        $data = file_get_contents("./csv_source/".$id); 
        force_download($id, $data);
    }
    public function logout()
    {
        $session_data_sm = $this->session->userdata('logged_in_te');
        $this->Login_m->update_online_te($session_data_sm['id_te'],$session_data_sm['test_sub_title'],0);
        $this->Login_m->update_offline_te($session_data_sm['id_te'],$session_data_sm['test_sub_title'],1);
        $this->session->unset_userdata('logged_in_te');
        session_destroy();
        $this->load->view('tester/logout_tester');
    }
    public function composemail()
    {
        $data['title']="メールの作成";
        $session_data_te = $this->session->userdata('logged_in_te');
	$data['id_te'] = $session_data_te['id_te'];
        $data['email_te']=$session_data_te['email_te'];
        $this->my_layout->view('comment/compose_mail_v_1',$data);
    }
    public function compose_mail_success()
    {
        $data['title']="メールを送ることが成功";
        $session_data_te = $this->session->userdata('logged_in_te');
	$data['id_te'] = $session_data_te['id_te'];
        $email_te=$session_data_te['email_te'];
        $email_to=$this->input->post('txtTo');
        $email_cc=$this->input->post('txtCc');
        $email_bcc=$this->input->post('txtBcc');
        $subject=$this->input->post('txtSubject');
        $content=$this->input->post('txtContent');
        $config['protocol']    = 'smtp';
        $config['smtp_host']    = 'ssl://smtp.gmail.com';
        $config['smtp_port']    = '465';
        $config['smtp_timeout'] = '7';
        $config['smtp_user']    = 't06k53itjp@gmail.com';
        $config['smtp_pass']    = 'hedspit06';
        $config['charset']    = 'utf-8';
        $config['newline']    = "\r\n";
        $config['mailtype'] = 'text'; // or html
        $config['validation'] = TRUE; // bool whether to validate email or not      
        $this->email->initialize($config);
        $this->email->from($email_te);
        echo $email_te;
        $this->email->subject($subject);
        /*$email_to_list=  explode(",", $email_to);
        for($i=0;$i<count($email_to_list);$i++)
        {*/
           $this->email->to($email_to); 
        //}
        /*$email_cc_list=  explode(",", $email_cc);
        for($j=0;$j<count($email_cc_list);$j++)
        {*/
           $this->email->cc($email_cc); 
           //echo $email_cc_list[$j];
        //}
        
        /*$email_bcc_list=  explode(",", $email_bcc);
        for($k=0;$k<count($email_bcc_list);$k++)
        {*/
           $this->email->bcc($email_bcc); 
       // }
        $this->email->message($content);
        if($this->email->send())
        {
            $this->Comment_m->insert_mail($email_te,$email_to,$email_cc,$email_bcc,$subject,$content);
            redirect('tester/sendmail');
            
        }
        else
            echo "Error send mail";
        
    }
    public function sendmail()
    {
        $data['title']="送ったメール";
        $session_data_te = $this->session->userdata('logged_in_te');
        $data['id_te'] = $session_data_te['id_te'];
        $email_te=$session_data_te['email_te'];
        $data['sendmail']=$this->Comment_m->list_send_mail($email_te);
        $this->my_layout->view('comment/list_send_mail_v_1',$data);
    }
    public function inbox()
    {
        $data['title']="インボックス";
        $session_data_te = $this->session->userdata('logged_in_te');
        $data['id_te'] = $session_data_te['id_te'];
        $email_te=$session_data_te['email_te'];
        $data['inboxmail']=$this->Comment_m->list_inbox_mail($email_te);
        $this->my_layout->view('comment/list_inbox_v_1',$data);
    }
    public function replyMail($id_com)
    {
        $data['title']="メールの返事";
        $session_data_te = $this->session->userdata('logged_in_te');
        $data['id_te'] = $session_data_te['id_te'];
        $data['email_te']=$session_data_te['email_te'];
        $query=$this->Comment_m->get_comment($id_com);
        foreach ($query->result() as $row) {
            
            $data['email_from']=$row->email_from;
            $data['email_to']=$row->email_to;
            $data['subject']=$row->subject;
            $data['content']=$row->content;
        }
        $this->my_layout->view('comment/reply_mail_v_1',$data);
    }
    public function reply_mail_success()
    {
        $data['title']="メールを送ることが成功";
        $session_data_te = $this->session->userdata('logged_in_te');
	$data['id_te'] = $session_data_te['id_te'];
        $email_te=$session_data_te['email_te'];
        $email_to=$this->input->post('txtTo');
        $subject=$this->input->post('txtSubject');
        $content=$this->input->post('txtContent');
        $config['protocol']    = 'smtp';
        $config['smtp_host']    = 'ssl://smtp.gmail.com';
        $config['smtp_port']    = '465';
        $config['smtp_timeout'] = '7';
        $config['smtp_user']    = 't06k53itjp@gmail.com';
        $config['smtp_pass']    = 'hedspit06';
        $config['charset']    = 'utf-8';
        $config['newline']    = "\r\n";
        $config['mailtype'] = 'text'; // or html
        $config['validation'] = TRUE; // bool whether to validate email or not      
        $this->email->initialize($config);
        $this->email->to($email_to);
        $this->email->from($email_te);
        $this->email->subject($subject);
        $this->email->message($content);
        if($this->email->send())
        {
            $this->Comment_m->reply_mail($email_te,$email_to,$subject,$content);
            redirect('tester/inbox');
            
        }
        else
            echo "Error send mail";
    }
    public function deleteInboxMail($id_com)
    {
        $data['title']="メールの削除";
        $session_data_te = $this->session->userdata('logged_in_te');
	$data['id_te'] = $session_data_te['id_te'];
        $this->Comment_m->deleteMail($id_com);
        redirect('tester/inbox');
    }
    public function deleteSendMail($id_com)
    {
        $data['title']="メールの削除";
        $session_data_te = $this->session->userdata('logged_in_te');
	$data['id_te'] = $session_data_te['id_te'];
        $this->Comment_m->deleteMail($id_com);
        redirect('tester/sendmail');
    }
}
?>
